package com.mijie.query;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.mijie.homi.search.service.index.MoodUserIndexService;
import com.mijie.homi.search.util.DbNdsController;
import com.mijie.homi.search.util.Dbcontroller;
import com.mijie.homi.search.util.listUtil;


public class upUserid {

	/**
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		String sql = "select * from mood4full where userid=0 order by id desc limit 1";
		Dbcontroller dbc = new DbNdsController();
		ResultSet rs = dbc.getrs(sql);
		String uid = null ;
		String mid = null;
		while (rs.next()) {
			    mid = rs.getString("mid");
			uid = getUidByMid(mid, rs
					.getString("fromx"));
		}
		
	int n=	upUid(uid,mid);
	logger.info("  upUid uid:"+uid+" mid:"+mid+ " rst:"+n );
	if(n==0)
		logger.warn("  upUid uid:"+uid+" mid:"+mid);
	}

	private static int upUid(String uid, String mid) {
		 
		String sql = "update mood4full set userid="+uid+" where mid="+mid;
		return Dbcontroller.execx(sql);
	}

	private static String getUidByMid(String mid, String fromx) {
		Dbcontroller dbc = new DbNdsController();
		if (fromx.equals("sincinlib")) {
			String moodIds_str = mid;

			String sql = "select *,u.id uid from mood m,user u  where  m.userid=u.id and m.id in (@ids) order by m.id desc limit 100";
			sql = sql.replaceAll("@ids", moodIds_str);
			ResultSet rs = dbc.getrs(sql);
			try {
				while (rs.next()) {
					String id = rs.getString("uid");
					return id;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			// nbp
			return null;
		} else
			return getUidByMid4prblm(mid);

	}

	public static Logger logger = Logger.getLogger(upUserid.class);

	private static String getUidByMid4prblm(String mid) {
		try {
			Dbcontroller dbc = new DbNdsController();

			String prblmIds_str = mid;
			String sql = "select * from topic m,user u  where  m.author=u.id and m.id in (@ids) order by m.id desc limit 100";
			sql = sql.replaceAll("@ids", prblmIds_str);
			logger.info(sql);
			ResultSet rs = dbc.getrs(sql);
			while (rs.next()) {
				String id = rs.getString("author");
				return id;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
